package com.zx.timer;

import java.util.List;

import com.zx.bean.SendTaskDAOImpl;
import com.zx.commonsms.bean.SmsResultBean;
import com.zx.commonsms.bean.Task;
import com.zx.commonsms.util.CommonSmsUtil;
import com.zx.commonsms.util.StringUtil;
import com.zx.service.SendTaskService;

public class MyTask extends java.util.TimerTask {
	private SendTaskService sendTaskService;
	private CommonSmsUtil util;
	private static boolean isRunning = false;
	private static boolean flag = true;
    private SendTaskDAOImpl sendTaskDAOImpl;
	public void run() {
		System.out.println(new java.util.Date());
		// ontext.log("开始执行指定任务111");
		if (!isRunning) {
			// System.out.println(C_SCHEDULE_HOUR);
			// if (C_SCHEDULE_HOUR == cal.get(Calendar.HOUR_OF_DAY) && flag) {
			if (flag) {
				isRunning = true;
				// context.log("开始执行指定任务");
				// TODO 此处添加自定义的详细任务
				sendtask();
				isRunning = false;
				flag = false;
				// context.log("指定任务执行结束");
			}
		} else {
			// context.log("上一次任务执行还未结束");
		}
		// if (C_SCHEDULE_HOUR != cal.get(Calendar.HOUR_OF_DAY)) {
		flag = true;
		// }

	}

	public void test() {
		Task task = new Task();
		task.setName("28测试3");
		task.setPhones("13513111027");
		task.setContent("发送测试修改");
		CommonSmsUtil util = new CommonSmsUtil();
		SmsResultBean re = util.sendSms(task);
		System.out.println("");
	}

	public void sendtask() {
		try {
			//List<Object[]> list = sendTaskService.getSendTask();
			List<Object[]> list = sendTaskDAOImpl.findSendTask();
			for (int i = 0; i < list.size(); i++) {
				Object[] o = list.get(i);
				Task task = new Task();
				task.setPhones(StringUtil.ObjectToString(o[1]));
				task.setName("测试111");
				task.setContent(StringUtil.ObjectToString(o[2]));
				SmsResultBean re = util.sendSms(task);
				sendTaskService.setSendTask(StringUtil.ObjectToString(o[1]));

			}

		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
